import {combineReducers} from 'redux-immutable';
import * as actions from './actions';
import Immutable from 'immutable';
import {
    GET_CURRENT_USER_PAGE_PERMISSIONS_BEGIN,
    GET_CURRENT_USER_PAGE_PERMISSIONS_SUCCESS,
    GET_CURRENT_USER_PAGE_PERMISSIONS_FAIL
} from 'Shared/actions/currentUserContext';
import {DEFAULT_QUERY_OPTION} from './constants';
import {generateRowSpanData} from './utils';

const initData = (state, action) => {
    switch(action.type) {
        case actions.GET_INIT_DATA_BEGIN:
            return state.merge({
                isFetching: true,
                message: '',
            });
        case actions.GET_INIT_DATA_SUCCESS:
            return state.merge({
                isFetching: false,
                message: '',
                data: action.data.content
            });
        case actions.GET_INIT_DATA_FAIL:
            return state.merge({
                isFetching: false,
                message: action.message
            });
        default:
            return state;
    }
};
const listType = (state, action) => {
    switch(action.type) {
        case actions.GET_LIST_DATA_BEGIN:
            return state.merge({
                isFetching: true,
                hasError: '',
            });
        case actions.GET_LIST_DATA_SUCCESS:
            return state.merge({
                isFetching: false,
                hasError: '',
                data: action.data.content
            });
        case actions.GET_LIST_DATA_FAIL:
            return state.merge({
                isFetching: false,
                data: [],
                hasError: action.message,
            });
        default:
            return state;
    }
};
const listRate = (state, action) => {
    switch(action.type) {
        case actions.GET_LIST_DATA_BEGIN:
            return state.merge({
                isFetching: true,
                hasError: '',
            });
        case actions.GET_LIST_RATE_DATA_SUCCESS:
            return state.merge({
                isFetching: false,
                hasError: '',
                data: action.data.content
            });
        case actions.GET_LIST_DATA_FAIL:
            return state.merge({
                isFetching: false,
                data: [],
                hasError: action.message,
            });
        default:
            return state;
    }
};
const listMonth = (state, action) => {
    switch(action.type) {
        case actions.GET_LIST_DATA_BEGIN:
            return state.merge({
                isFetching: true,
                hasError: '',
            });
        case actions.GET_LIST_MONTH_DATA_SUCCESS:
            return state.merge({
                isFetching: false,
                hasError: '',
                data: generateRowSpanData(action.data.content, 'channel', ['channel'])
            });
        case actions.GET_LIST_DATA_FAIL:
            return state.merge({
                isFetching: false,
                data: [],
                hasError: action.message,
            });
        default:
            return state;
    }
};
const listCountry = (state, action) => {
    switch(action.type) {
        case actions.GET_LIST_COUNTRY_DATA_BEGIN:
            return state.merge({
                isFetching: true,
                hasError: '',
            });
        case actions.GET_LIST_COUNTRY_DATA_SUCCESS:
            return state.merge({
                isFetching: false,
                hasError: '',
                // eslint-disable-next-line array-callback-return
                data: action.data.content.map(item =>
                    Object.assign({}, item, {
                        details: generateRowSpanData(item.details, 'channel', ['channel'])
                    }))
            });
        case actions.GET_LIST_COUNTRY_DATA_FAIL:
            return state.merge({
                isFetching: false,
                data: [],
                hasError: action.message,
            });
        default:
            return state;
    }
};
const listWeek = (state, action) => {
    switch(action.type) {
        case actions.GET_LIST_WEEK_DATA_BEGIN:
            return state.merge({
                isFetching: true,
                hasError: '',
            });
        case actions.GET_LIST_WEEK_DATA_SUCCESS:
            return state.merge({
                isFetching: false,
                hasError: '',
                data: Object.assign({}, action.data.content, {
                    fistWeekDetails: generateRowSpanData(action.data.content.fistWeekDetails, 'dealerCode', ['dealerCode', 'country', 'channel']),
                    weekDetails: generateRowSpanData(action.data.content.weekDetails, 'dealerCode', ['dealerCode', 'country', 'channel']),
                })
            });
        case actions.GET_LIST_WEEK_DATA_FAIL:
            return state.merge({
                isFetching: false,
                data: [],
                hasError: action.message,
            });
        default:
            return state;
    }
};
export const permission = (state, action) => {
    switch(action.type) {
        case GET_CURRENT_USER_PAGE_PERMISSIONS_BEGIN:
            return state.merge(Immutable.fromJS({
                isFetching: true,
                message: ''
            }));
        case GET_CURRENT_USER_PAGE_PERMISSIONS_SUCCESS:
            return state.merge(Immutable.fromJS({
                isFetching: false,
                message: '',
                data: action.data
            }));
        case GET_CURRENT_USER_PAGE_PERMISSIONS_FAIL:
            return state.merge(Immutable.fromJS({
                isFetching: false,
                message: action.message
            }));
        default:
            return state;
    }
};
const appState = (state, action) => {
    switch(action.type) {
        case actions.SAVE_QUERY_CONDITION:
            return state.setIn(['queryCondition', action.name], action.value);
        case actions.RESET_QUERY_PANEL:
            return state.set('queryCondition', Immutable.fromJS(DEFAULT_QUERY_OPTION));
        default:
            return state;
    }
};
const domainData = combineReducers({
    initData,
    listRate,
    listType,
    listMonth,
    listCountry,
    listWeek,
    permission,
});

export default combineReducers({
    domainData,
    appState,
});
